/*
 * Copyright (c) 2017, AEye
 * All rights reserved.
 */

#ifndef NONBLOCK_TCP_SOCKET_H
#define NONBLOCK_TCP_SOCKET_H

#include <cstddef>
#include <cstdint>
#include <string>

#ifdef _MSC_VER
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif
class NonBlockTCPSocket {

  bool _isOpen;
  int  fd;
  int  getSO_ERROR();
  int  setNonblockingAttribute();
#ifdef _MSC_VER
  std::string GetLastErrorAsString(std::string file, std::string function, uint16_t line);
#else
  std::string GetLastErrorAsString(std::string file, std::string function, size_t line);
#endif
 
  int GetLastError();
  ssize_t sendWithSelect(const void* buff, size_t buffSize, size_t mSec);
  ssize_t receiveWithSelect(void* buff, size_t buffSize, size_t mSec);
public:
  bool isOpen() const;
  bool isConnected();
  void cleanUp(size_t mSec);
  void close();
  bool connect(const std::string& addr, uint16_t port, size_t timeoutSec);
  size_t send(const void* buff, size_t buffSize, size_t mSec);
  size_t receive(void* buff, size_t buffSize, size_t mSec);

  NonBlockTCPSocket();
  virtual ~NonBlockTCPSocket();
};

#endif //NONBLOCK_TCP_SOCKET_H
